package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.service.system.DeptService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        String companyId = getLoginUserCompanyId();
        PageInfo<Dept> pageInfo = deptService.findByPage(pageNum,pageSize,companyId);
        request.setAttribute("pageInfo",pageInfo);
        return "system/dept/dept-list";
    }

    /*
      url:/system/dept/list.do
      作用： 进入添加页面,存储域数据：企业的所有部门
      参数： 无
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        String companyId = getLoginUserCompanyId();
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);
        return "system/dept/dept-add";
    }

    /*
      url:/system/dept/edit.do
      作用： 有传部门id时为修改信息，没有传部门id时为添加部门信息
      参数： Dept dept
     */
    @RequestMapping("/edit")
    public String edit(Dept dept){
        //补全部门信息
        String companyId = getLoginUserCompanyId();
        String companyName = getLoginUserCompanyName();
        dept.setCompanyId(companyId);
        dept.setCompanyName(companyName);

        String id = dept.getId();
        if (StringUtils.isEmpty(id)){
            //新增部门信息
            deptService.save(dept);
        }else {
            deptService.update(dept);
        }
        return "redirect:/system/dept/list.do";
    }


    /*
      url:/system/dept/toUpdate.do?id=3d00290a-1af0-4c28-853e-29fbf96a2722
      作用： 进入编辑页面,根据部门id查找部门信息，数据回显
      参数： 无
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Dept dept = deptService.findById(id);
        request.setAttribute("dept",dept);

        String companyId = getLoginUserCompanyId();
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);
        return "system/dept/dept-update";
    }



    /*
      url:/system/dept/delete.do?id=100
      作用： 删除部门
      参数： 部门id
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        HashMap<String, Object> resultMap = new HashMap<>();
        Boolean flag = deptService.delete(id);
        if (flag){
            resultMap.put("message","删除成功");
        }else {
            resultMap.put("message","有子部门，删除失败");
        }
        resultMap.put("flag",flag);
        return resultMap;
    }


}
